-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11 - part1.py
More file actions
68 lines (52 loc) · 1.65 KB
/
Copy pathday11 - part1.py
File metadata and controls
68 lines (52 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
file = open("input.txt")
seats = [list(line.replace("\n", "")) for line in file.readlines()]
last = [row[:] for row in seats]
floor = "."
empty = "L"
occupied = "#"
def isOccupied(seats, x, y):
if x < 0 or y < 0:
return False
try:
return seats[y][x] == occupied
except IndexError:
return False
def isEmpty(seats, x, y):
if x < 0 or y < 0:
return True
try:
return seats[y][x] != occupied
except IndexError:
return True
def shouldBeOccupied(seats, x, y):
toTry = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
return len(
[test for test in toTry if not isOccupied(seats, x + test[0], y + test[1])]
) == len(toTry)
def shouldBeEmpty(seats, x, y):
toTry = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)]
return (
len([test for test in toTry if not isEmpty(seats, x + test[0], y + test[1])])
>= 4
)
def showMap(seats):
for line in seats:
print("".join(line))
print()
while True:
for y in range(len(seats)):
for x in range(len(seats[y])):
if last[y][x] == floor:
continue
if last[y][x] == empty:
seats[y][x] = occupied if shouldBeOccupied(last, x, y) else empty
if last[y][x] == occupied:
seats[y][x] = empty if shouldBeEmpty(last, x, y) else occupied
# showMap(seats)
if last == seats:
break
last = [row[:] for row in seats]
res = 0
for seat in seats:
res += len([s for s in seat if s == occupied])
print(res)